﻿
// DrawView.cpp: CDrawView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "Draw.h"
#endif

#include "DrawDoc.h"
#include "DrawView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CDrawView

IMPLEMENT_DYNCREATE(CDrawView, CView)

BEGIN_MESSAGE_MAP(CDrawView, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

// CDrawView 构造/析构

CDrawView::CDrawView() noexcept
{
	// TODO: 在此处添加构造代码
	m_ptOrigin = 0;
	m_ptOld = 0;
	m_bDraw = false;
}

CDrawView::~CDrawView()
{
}

BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CDrawView 绘图

void CDrawView::OnDraw(CDC* /*pDC*/)
{
	CDrawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码
}


// CDrawView 打印

BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CDrawView 诊断

#ifdef _DEBUG
void CDrawView::AssertValid() const
{
	CView::AssertValid();
}

void CDrawView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CDrawDoc* CDrawView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
	return (CDrawDoc*)m_pDocument;
}
#endif //_DEBUG


// CDrawView 消息处理程序


void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	//MessageBox("Drawview clicked!");
	m_ptOrigin = m_ptOld = point;
	m_bDraw = true;
	CView::OnLButtonDown(nFlags, point);
}


void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	/*HDC hdc;
	hdc = ::GetDC(m_hWnd); // 画图需要获取hdc
	MoveToEx(hdc, m_ptOrigin.x, m_ptOrigin.y, nullptr);
	LineTo(hdc, point.x, point.y);
	::ReleaseDC(m_hWnd, hdc); // 画完需要释放dc*/  // 窗口相关的再CWnd，画图相关的再CDC中

	/*CDC* pDC = GetDC();
	pDC->MoveTo(m_ptOrigin);
	pDC->LineTo(point);
	ReleaseDC(pDC);
	*/

	//CClientDC dc(this);
	//CClientDC dc(this->GetParent());
	//dc.MoveTo(m_ptOrigin);
	//dc.LineTo(point);


	//CWindowDC dc(GetDesktopWindow());
	//dc.MoveTo(m_ptOrigin);
	//dc.LineTo(point);

	// 画笔的创建
	//CPen pen(PS_DOT, 2, RGB(0, 255, 0));
	//CClientDC dc(this);
	//CPen* pOldPen = dc.SelectObject(&pen);
	//dc.MoveTo(m_ptOrigin);
	//dc.LineTo(point);
	//dc.SelectObject(pOldPen);

	// 画刷的使用 
	//CBrush brush(RGB(255, 0, 0));
	//CClientDC dc(this);
	//dc.FillRect(CRect(m_ptOrigin, point), &brush);


	// 位图的使用
	//CBitmap bitmap;
	//bitmap.LoadBitmap(IDB_BITMAP1);// 这里怎么重命名这个资源的名称
	//CBrush brush(&bitmap);
	//CClientDC dc(this);
	//dc.FillRect(CRect(m_ptOrigin, point), &brush);


	//CClientDC dc(this);
	//CBrush* pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));// 该函数是静态函数，所以直接通过类来调用
	//CBrush* pOldBrush = dc.SelectObject(pBrush);
	//dc.Rectangle(CRect(m_ptOrigin, point)); // 这样会直接隐藏以前的图案
	//dc.SelectObject(pOldBrush);

	m_bDraw = false;

	CView::OnLButtonUp(nFlags, point);
}


void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CClientDC dc(this);
	CPen pen(PS_SOLID, 2, RGB(255, 0, 0));
	CPen* pOldPen = dc.SelectObject(&pen);
	if (m_bDraw) {
		//dc.SetROP2(R2_BLACK);// 绘画模式
		dc.MoveTo(m_ptOrigin);
		//dc.LineTo(point);
		dc.LineTo(m_ptOld);
		dc.MoveTo(m_ptOrigin);
		dc.MoveTo(m_ptOld);
		dc.LineTo(point);
		//m_ptOrigin = point;
		m_ptOld = point;
	}
	dc.SelectObject(pOldPen);
	CView::OnMouseMove(nFlags, point);
}
